<?php

$filename = 'names.txt';
$file = fopen($filename, 'r');
$data = fread($file, filesize($filename));
fclose($file);

$strippedTags = strip_tags($data);
$months = explode('[upraviť]', $strippedTags);

$nameList = array();
foreach ($months as $monthKey => $month) {
	if ($month) {
		$nameList[$monthKey] = array();
		$days = explode("\n", $month);

		foreach ($days as $dayKey => $day) {
			if ($dayKey == 0 || $dayKey == 1) {
				continue;
			} else {
				$names = explode(',', $day);
				if ($names) {
					foreach ($names as $name) {
						$name = trim($name);
						if ($name != '' && $name != '-') {
							$nameList[$monthKey][$dayKey - 1][] = $name;
						}
					}
				}
			}
		}
	}
}

foreach ($nameList as $month => $day) {
	$query = 'INSERT INTO mnhis_name (name, day, month) VALUES';
	$queryParts = array();
	foreach ($day as $key => $names) {
		foreach ($names as $name) {
			$queryParts[] = '("' . $name . '", ' . $key . ', ' . $month . ')';
		}
	}
	$query .= implode(', ', $queryParts) . ';';
	//echo $query . "\n";
}

?>